<?php

class Unit_traffic extends CI_Model {

	function __construct() {
		parent::__construct();
	}

	function bandwidth($cond) {
		$data['updated_at >='] = $cond['start_date'];
		$data['updated_at <='] = $cond['end_date'];
		$this->db->where($data);
		$this->db->where_in('unit_id', $cond['unit_id']);
		$this->db->group_by('updated_at');
		$this->db->select('sum(traffic) as traffic, updated_at');
		$query = $this->db->get('unit_traffic');
		$traffic = $query->result_array();
		$r = array();
		$r['info'] = 'bandwidth(Mbps)';
		$r['peak'] = 0;
		$r['peak_time'] = '';
		$r['charge_bandwidth'] = $this->charge_bandwidth($cond);
		$r['data'] = array();
		$r['total'] = 0;
		foreach ($traffic as $v) {
			$b = number_format($v['traffic']/(1024*1024*300), 2, '.', '');
			if ($b > $r['peak']) {
					$r['peak_time'] = $v['updated_at'];
					$r['peak'] = $b;
			}
			$r['data'][] = array(strtotime($v['updated_at'])*1000, $b);
			$r['total'] += $v['traffic'];
		}
		$r['total'] = number_format($r['total']/(1024*1024*300), 2, '.', '') . 'Mbps';
		return array($r);
	}

	function charge_bandwidth($cond) {
		$data['updated_at >='] = $cond['start_date'];
		$data['updated_at <='] = $cond['end_date'];
		$this->db->where($data);
		$this->db->where_in('unit_id', $cond['unit_id']);
		$this->db->group_by('updated_at');
		$this->db->select('sum(traffic) as traffic, updated_at');
		$this->db->order_by('traffic', 'desc');
		$query = $this->db->get('unit_traffic');
		$traffic = $query->result_array();
		$p = ceil(0.05*count($traffic));
		if (!isset($traffic[$p])) {
			return array('traffic' => 0, 'updated_at' => '1970-01-01');
		}
		$traffic[$p]['traffic'] = number_format($traffic[$p]['traffic']/(1024*1024*300), 2, '.', '') . 'Mbps';
		return $traffic[$p];
	}

	function bandwidth_table($cond) {
		$data = array();
		$r = array();
		$t = array();
		$data['updated_at >='] = $cond['start_date'];
		$data['updated_at <='] = $cond['end_date'];
		foreach($cond['unit_id'] as $u) {
			$this->db->where('status', 1);
			$this->db->where('id', $u);
			$query = $this->db->get('unit');
			$unit = $query->result_array();
			$data['unit_id'] = $u;
			$this->db->where($data);
			$query = $this->db->get('unit_traffic');
			$r[$u]['info'] = $unit[0];
			$r[$u]['real_data'] = $query->result_array();
			$r[$u]['total'] = 0;
			$r[$u]['peak'] = 0;
			
			foreach ($r[$u]['real_data'] as $k => $v) {
				$b = intval($v['traffic']/(1024*8*300));
				$r[$u]['total'] += $b;
				if ($b > $r[$u]['peak']) {
					$r[$u]['peak_time'] = $v['updated_at'];
					$r[$u]['peak'] = $b;
				}
			}
			unset($r[$u]['real_data']);
			array_push($t, $r[$u]);
		}
		return $t;
	}


	function flow($cond) {
		$data = array();
		$r = array();
		$t = array();
		$data['updated_at >='] = $cond['start_date'];
		$data['updated_at <='] = $cond['end_date'];
		foreach($cond['unit_id'] as $u) {
			$this->db->where('status', 1);
			$this->db->where('id', $u);
			$query = $this->db->get('unit');
			$unit = $query->result_array();
			$data['unit_id'] = $u;
			$this->db->where($data);
			$query = $this->db->get('unit_traffic');
			$r[$u]['info'] = $unit[0];
			$r[$u]['real_data'] = $query->result_array();
			$sum = 0;
			$peak = 0;
			$peak_time = '';
			foreach ($r[$u]['real_data'] as $k => $v) {
				$b = intval($v['traffic']/(1024*8*300));
				if ($b > $peak) {
					$peak_time = $v['updated_at'];
					$peak = $b;
				}
				$sum += $b;
				$sec = strtotime($v['updated_at']);
				if ($sec%(60*60*24) == 0) {
					$r[$u]['data'][] = array(strtotime($v['updated_at'])*1000,  $sum);
					$tr = array('flow' => $sum, 'peak' => $peak, 'peak_time' => $peak_time, 'date' => substr($v['updated_at'], 0, 10));
					$r[$u]['tr'][] = $tr;
					$sum = 0;
					$peak = 0;
					$peak_time = '';
				}
			}
			unset($r[$u]['real_data']);
			array_push($t, $r[$u]);
			unset($r);
		}
		return $t;
	}

}
